<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <!-- Meta, title, CSS, favicons, etc. -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>忘了密码页面 </title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome -->
    <link href="css/font-awesome.min.css" rel="stylesheet">
    <!-- NProgress -->
    <!--<link href="css/nprogress.css" rel="stylesheet">-->
    <link href="css/button.css" rel="stylesheet">
    <!-- Animate.css -->
    <link href="https://colorlib.com/polygon/gentelella/css/animate.min.css" rel="stylesheet">

    <!-- Custom Theme Style -->
    <link href="css/custom.min.css" rel="stylesheet">
</head>

<body class="login">
<div>
    <a class="hiddenanchor" id="signup"></a>
    <a class="hiddenanchor" id="signin"></a>

    <div class="login_wrapper">
        <div class="animate form login_form">
            <section class="login_content">
                <form id="go" action="login.html" >
                    <h1>找回密码</h1>
                    <div>
                        <input style="margin:0" type="text" class="form-control" id="userName" placeholder="请输入用户名" required="" />
                        <label style="color: red;" id="userNameError"></label>
                    </div>

                    <div>
                        <input  type="tel" class="form-control" id="phone" placeholder="请输入手机号码" required="" />
                        <label style="color: red;" id="phoneError"></label>
                    </div>


                    <div id="code1" style="display: none">
                        <input  type="text" id="code" class="form-control" placeholder="请输入手机验证码" required="" style="width: 180px;display: inline-block;"/>
                        <button style="height:34px;width: 161px" id="numbtn">获取验证码</button>
                        <label style="color: red;" id="codeError"></label>
                        <label style="color: green;" id="yzError"></label>
                    </div>
                    <div id="pwd1" style="display: none">
                        <input style="margin:0" type="password" id="password" class="form-control" placeholder="请设置新密码" required="" />
                        <label style="color: red;" id="pwdError"></label>
                    </div>
                    <div id="pwd2" style="display: none">
                        <input style="margin:0" type="password" id="rePassword"  class="form-control" placeholder="请确认密码" required="" />
                        <label style="color: red;" id="rePwdError"></label>
                    </div>
                    <div>
                        <a  class="btn btn-default submit"  id="miss" href="#">确认</a>
                        <a  class="btn btn-default submit" href="login.html">返回</a>
                    </div>

                    <div class="clearfix"></div>

                    <div class="separator">

                        <div class="clearfix"></div>
                        <br />

                        <div>
                            <h1><i class="fa fa-paw"></i>美的&nbsp;&nbsp;&nbsp;集团</h1>
                            <p>©2019 All Rights Reserved. </p>
                        </div>
                    </div>
                </form>
            </section>
        </div>

        <div id="register" class="animate form registration_form">
            <section class="login_content">
                <form>
                    <h1>Create Account</h1>
                    <div>
                        <input type="text" class="form-control" placeholder="Username" required="" />
                    </div>
                    <div>
                        <input type="email" class="form-control" placeholder="Email" required="" />
                    </div>
                    <div>
                        <input type="password" class="form-control" placeholder="Password" required="" />
                    </div>
                    <div>
                        <a class="btn btn-default submit" href="index.html">Submit</a>
                    </div>

                    <div class="clearfix"></div>

                    <div class="separator">

                        <div class="clearfix"></div>
                        <br />

                        <div>
                            <h1><i class="fa fa-paw"></i>美的&nbsp;&nbsp;&nbsp;集团</h1>
                            <p>©2019 All Rights Reserved. </p>
                        </div>
                    </div>
                </form>
            </section>
        </div>
    </div>
</div>

<script src="js/jquery.min.js"></script>

<script>
    var countdown = 60;

    $('#numbtn').on('click',function(){

        var obj = $("#numbtn");

        settime(obj);

    })

    function settime(obj) { //发送验证码倒计时

        if (countdown == 0) {

            obj.attr('disabled', false);

//obj.removeattr("disabled");

            obj.html("免费获取验证码");

            countdown = 60;

            return;

        } else {

            obj.attr('disabled', true);

            obj.html("重新发送(" + countdown + ")");

            countdown--;

        }

        setTimeout(function () {

                settime(obj)

            }

            , 1000)

    }

    $(function  () {
        var flag = true;
        var yanz = null;
        var uPattern = /^[a-zA-Z0-9_-]{3,16}$/;
        var pPattern = /^[\w_-]{6,16}$/;
        $("#userName").blur(function(){
            var $userName = $("#userName").val();
            if ($userName == ""){
                $("#userNameError").html("用户名不能为空");
            }else if (!(uPattern.test($userName))){
                $("#userNameError").html("请输入正确的用户名");
            }else {
                $("#userNameError").html("");
            }
        });
        $("#phone").blur(function(){
            var $phone = $("#phone").val();
            if ($phone == "" ){
                $("#code1").hide();
                $("#code").val("");
                $("#pwd1").hide();
                $("#pwd2").hide();
                $("#password").val("");
                $("#rePassword").val("");
                $("#phoneError").html("手机号码不能为空");
            }else if (!(/^1[34578]\d{9}$/.test($phone))){
                $("#code1").hide();
                $("#code").val("");
                $("#pwd1").hide();
                $("#pwd2").hide();
                $("#password").val("");
                $("#rePassword").val("");
                $("#phoneError").html("请输入正确的手机号码");
            }else {
                $("#code1").show();
                $("#phoneError").html("");
            }
        });
        $("#code").blur(function(){
            var $code = $("#code").val();
            if ($code == ""){
                $("#pwd1").hide();
                $("#pwd2").hide();
                $("#password").val("");
                $("#rePassword").val("");
                $("#codeError").html("验证码不能为空");
                $("#yzError").html("");
            }else {
                $("#pwd1").show();
                $("#pwd2").show();
                $("#codeError").html("");
            }
        });
        $("#password").blur(function(){
            var $password = $("#password").val();
            if ($password == ""){
                $("#pwdError").html("密码不能为空");
            }else if (!(pPattern.test($password))){
                $("#pwdError").html("请输入正确的密码");
            }else {
                $("#pwdError").html("");
            }
        });
        $("#rePassword").blur(function(){
            var $password = $("#password").val();
            var $rePassword = $("#rePassword").val();
            if ($rePassword == ""){
                $("#rePwdError").html("密码不能为空");
            }else if($rePassword != $password){
                $("#rePwdError").html("密码请输入一致");
            }else {
                $("#rePwdError").html("");
            }
        });


        $("#numbtn").click(function () {
            var $userName = $("#userName").val();
            var $phone = $("#phone").val();
            $.ajax({
                type:"post",
                xhrFields:{withCredentials:true},
                url:"http://localhost:8081/first/getCode",
                dataType:"json",
                data:{"userName":$userName,"phone":$phone},
                success:function(data) {
                    if(data.message =="yes"){
                        $("#yzError").html("60秒内请接收短信");
                    }else if (data.message== "noPhone") {
                        alert("此用户电话号码输入不正确");
                    }else {
                        alert("发送失败");
                    }
                },
                error:function(){
                    alert("异常");
                }
            });
        });



        $("#miss").click(function(){
            flag = true;
            var $userName = $("#userName").val();
            var $phone = $("#phone").val();
            var $code = $("#code").val();
            var $password = $("#password").val();
            var $rePassword = $("#rePassword").val();
            if ($userName == "" || !(uPattern.test($userName))) {
                $("#userNameError").html("请输入正确的用户名");
                flag = false;
            }else if ($phone == "" || !(/^1[34578]\d{9}$/.test($phone))) {
                $("#phoneError").html("请输入电话号码");
                flag = false;
            }else if ($code == "") {
                $("#codeError").html("请输入正确的验证码");
                flag = false;
            }else if ($password == "" || !(pPattern.test($password))) {
                $("#pwdError").html("请输入正确的密码");
                flag = false;
            }else if ($rePassword == "" || $rePassword != $password) {
                $("#rePwdError").html("密码请输入一致");
                flag = false;
            }

            if (flag == true) {
                $.ajax({
                    type:"post",
                    xhrFields:{withCredentials:true},
                    url:"http://localhost:8081/first/miss",
                    dataType:"json",
                    data:{"userName":$userName,"phone":$phone,"pwd":$password,"code":$code},
                    success:function(data) {
                        if(data.message=="yes"){
                            alert("找回密码成功！");
                            window.location.href="login.html";
                        }else{
                            alert("请求失败");
                            $("#userName").val($userName);
                            $("#phone").val($phone);
                            $("#code").val($code);
                            $("#password").val($password);
                            $("#rePassword").val($rePassword);
                        }
                    },
                    error:function(){
                        alert("异常");
                    }
                });
            }else {
                alert("请输入正确的信息");
            }


        });
    })


</script>
</body>
</html>